﻿

#region Namespace imports

using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;


#endregion

namespace System.Search.Linq
{

    /// <summary>
    /// 
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Visualizer")]
    public partial class SearchIndexListQueryVisualizerForm : Form
    {
        #region Private members

        /// <summary>
        /// 
        /// </summary>
        private object index;

        /// <summary>
        /// 
        /// </summary>
        private object query;

        #endregion

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "caml")]
        public SearchIndexListQueryVisualizerForm(object index)
        {

            this.index = index;

            InitializeComponent();


            Init();
        }

        #endregion

        #region Helper methods

        /// <summary>
        /// Helper method to initialize the textboxes.
        /// </summary>
        private void Init()
        {
            query = index.GetType().GetProperty("Query", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(index, new object[] { });
            MethodInfo mi = (MethodInfo)query.GetType().GetMethod("MakeSQLStatement", BindingFlags.NonPublic | BindingFlags.Instance);
            string SQL = (string)mi.Invoke(query, new object[] { });

            // Fromat SQL satement to Visualize
            SQL = SQL.Replace("FROM", "\r\n\r\nFROM");
            SQL = SQL.Replace("WHERE", "\r\n\r\nWHERE");
            SQL = SQL.Replace("ORDER BY", "\r\n\r\nORDER BY");

            txtSystemIndexQuery.Text = SQL;

            // Show LINQ Expression

            string expressionStr = (string)index.GetType().GetProperty("ExpressionStr", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(index, new object[] { });
            txtLinq.Text = expressionStr;


        }

        #endregion

        #region Event handlers

        private void SystemIndexListQueryVisualizerForm_Load(object sender, EventArgs e)
        {
            //

        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            MethodInfo mi = (MethodInfo)query.GetType().GetMethod("ExecuteQueryDataSet", BindingFlags.NonPublic | BindingFlags.Instance);
            DataSet ds = (DataSet)mi.Invoke(query, new object[] { txtSystemIndexQuery.Text });
            if (ds.Tables.Count > 0)
                ResultsGridView.DataSource = ds.Tables[0];
            tabControl1.SelectedIndex = 1;

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

    }


}
